<# 
.SYNOPSIS 
	Shows all networks and their gateway, ID, and hosts mac
#>  
Function Get-FLvirtualNetworks {
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/networkService/v1.1/tenants/default/networks" 
	$JsonAnswer = Invoke-WebRequest $url
	$Status = $JsonAnswer.Content | ConvertFrom-Json
	Return $Status
}

<# 
.SYNOPSIS 
	Creates a new virtual network. Name and ID are required, gateway is optional
#>  
Function New-FLvirtualNetwork {
	[CmdletBinding()]
	param (
		[Parameter(Mandatory=$True,Position=1)][string]$Name,
		[Parameter(Mandatory=$True,Position=2)][string]$NetworkID,
		[Parameter(Mandatory=$False,Position=3)][string]$Gateway
	)
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/networkService/v1.1/tenants/default/networks/$NetworkID"
	If ($Gateway) {
	$body = @{
		"network" = @{
		"name" = $Name
		"gateway" = $Gateway;
		}
	} | ConvertTo-JSON
	} Else {
		$body = @{
		"network" = @{
		"name" = $Name;
		}
		} | ConvertTo-JSON
	}
	$Reply = Invoke-RestMethod -Method Put -Uri $url -Body $body
	Return $Reply
}

<# 
.SYNOPSIS 
	Removes a new virtual network
#>  
Function Remove-FLvirtualNetwork {
	[CmdletBinding()]
	param (
		[Parameter(Mandatory=$True,Position=1)][string]$NetworkID
	)
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/networkService/v1.1/tenants/default/networks/$NetworkID"
	$Reply = Invoke-RestMethod -Method Delete -Uri $url
	Return $Reply
}

<# 
.SYNOPSIS 
	Attaches a host to a virtual network
#>
Function Add-FLhostToVirtualNetwork {
	[CmdletBinding()]
	param (
		[Parameter(Mandatory=$True,Position=1)][string]$NetworkID,
		[Parameter(Mandatory=$True,Position=2)][string]$PortName,
		[Parameter(Mandatory=$True,Position=3)][string]$MacAddress
	)
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/networkService/v1.1/tenants/default/networks/$NetworkID/ports/$PortName/attachment"
	
	If (($MacAddress -contains ":") -eq $False) {
		$MacAddress = $MacAddress.Substring(0,2)+":"+$MacAddress.Substring(2,2)+":"+$MacAddress.Substring(4,2)+
		":"+$MacAddress.Substring(6,2)+":"+$MacAddress.Substring(8,2)+":"+$MacAddress.Substring(10,2)
	}
	
	$body = @{
		"attachment" = @{"id" = $NetworkID;
		"mac" = $MacAddress; }
	} | ConvertTo-JSON
	Invoke-RestMethod -Method Put -Uri $url -Body $body
}

<# 
.SYNOPSIS 
	Removes a host from a virtual network
#>
Function Remove-FLhostFromVirtualNetwork {
	[CmdletBinding()]
	param (
		[Parameter(Mandatory=$True,Position=1)][string]$NetworkID,
		[Parameter(Mandatory=$True,Position=2)][string]$PortName
	)
	If (!$FLControllerURL) { Connect-FLcontroller }
	$url = "$FLControllerURL/networkService/v1.1/tenants/default/networks/$NetworkID/ports/$PortName/attachment"
	
	If (($MacAddress -contains ":") -eq $False) {
		$MacAddress = $MacAddress.Substring(0,2)+":"+$MacAddress.Substring(2,2)+":"+$MacAddress.Substring(4,2)+
		":"+$MacAddress.Substring(6,2)+":"+$MacAddress.Substring(8,2)+":"+$MacAddress.Substring(10,2)
	}
	
	$body = @{
		"attachment" = @{"id" = $NetworkID;}
	} | ConvertTo-JSON
	Invoke-RestMethod -Method Delete -Uri $url -Body $body
}